package com.wangdao.wechatmall.service.admin;

import com.github.pagehelper.PageInfo;
import com.wangdao.wechatmall.bean.pojo.Keyword;
import com.wangdao.wechatmall.bean.pojo.KeywordExample;
import com.wangdao.wechatmall.bean.pojo.SearchHistory;
import com.wangdao.wechatmall.bean.pojo.SearchHistoryExample;
import com.wangdao.wechatmall.bean.vo.wx.HistoryKeyVO;
import com.wangdao.wechatmall.bean.vo.wx.SearchIndexVO;
import com.wangdao.wechatmall.mapper.KeywordMapper;
import com.wangdao.wechatmall.mapper.SearchHistoryMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * @author ShuiYi
 * @time 2021-07-12 11:21
 */
@Service
public class SearchServiceImpl implements SearchService {

    @Autowired
    SearchHistoryMapper searchHistoryMapper;
    @Autowired
    KeywordMapper keywordMapper;

    @Override
    public List<String> searchHelper(String keyword) {
        SearchHistoryExample searchHistoryExample = new SearchHistoryExample();

        SearchHistoryExample.Criteria criteria = searchHistoryExample.createCriteria();

        criteria.andKeywordLike("%" + keyword + "%");
        List<SearchHistory> histories = searchHistoryMapper.selectByExample(searchHistoryExample);
        ArrayList<String> strings = new ArrayList<>();
        for (SearchHistory history : histories) {
            strings.add(history.getKeyword());
        }
        return strings;
    }

    @Override
    public void clearHistory(Integer id) {
        SearchHistoryExample searchHistoryExample = new SearchHistoryExample();
        SearchHistoryExample.Criteria criteria = searchHistoryExample.createCriteria();
        criteria.andDeletedEqualTo(false);
        criteria.andUserIdEqualTo(id);

        SearchHistory history = new SearchHistory();
        history.setDeleted(true);
        searchHistoryMapper.updateByExampleSelective(history, searchHistoryExample);
    }

    @Override
    public SearchIndexVO index(Integer id) {

        SearchIndexVO searchIndexVO = new SearchIndexVO();

        SearchHistoryExample searchHistoryExample = new SearchHistoryExample();
        SearchHistoryExample.Criteria criteriaHistory = searchHistoryExample.createCriteria();
        criteriaHistory.andDeletedEqualTo(false);
        criteriaHistory.andUserIdEqualTo(id);
        List<SearchHistory> searchHistories = searchHistoryMapper.selectByExample(searchHistoryExample);

        KeywordExample keywordExample = new KeywordExample();
        KeywordExample.Criteria criteriaKey = keywordExample.createCriteria();
        keywordExample.setOrderByClause("update_time desc");
        List<Keyword> keywords = keywordMapper.selectByExample(keywordExample);


        List<HistoryKeyVO> historyWords = new ArrayList<>();
        List<Keyword> hotWords = new ArrayList<>();
        List<String> strings = new ArrayList<>();

        for (SearchHistory searchHistory : searchHistories) {
            if (!searchHistory.getDeleted()) {
                HistoryKeyVO historyKeyVO = new HistoryKeyVO();
                historyKeyVO.setKeyword(searchHistory.getKeyword());
                historyWords.add(historyKeyVO);
            }
        }
        for (Keyword keyword : keywords) {
            if (keyword.getIsDefault() && !keyword.getDeleted()) {
                searchIndexVO.setDefaultKeyword(keyword);
                break;
            }
        }
        for (Keyword keyword : keywords) {
            if (!keyword.getDeleted() && keyword.getIsHot()) {
                hotWords.add(keyword);
            }
        }

        searchIndexVO.setHistoryKeywordList(historyWords);
        searchIndexVO.setHotKeywordList(hotWords);

        return searchIndexVO;
    }
}
